<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="" prop="id">
        <el-input v-model="dataForm.id" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="赛项ID" prop="matchItemId">
        <el-select v-model="dataForm.matchItemId" placeholder="请选择">
          <el-option label="人人" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="代表队ID" prop="teamId">
        <el-select v-model="dataForm.teamId" placeholder="请选择">
          <el-option label="人人" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="参赛队名-代表队内不重复" prop="teamName">
        <el-input v-model="dataForm.teamName" placeholder="参赛队名-代表队内不重复"></el-input>
      </el-form-item>
      <el-form-item label="参赛时间" prop="signTime">
        <el-date-picker type="date" placeholder="参赛时间" value-format="yyyy-MM-dd" v-model="dataForm.signTime"></el-date-picker>
      </el-form-item>
      <el-form-item label="0未晋级 1晋级" prop="isRise">
        <ren-radio-group v-model="dataForm.isRise" dict-type="isRise"></ren-radio-group>
      </el-form-item>
      <el-form-item label="晋级时间" prop="riseTime">
        <el-input v-model="dataForm.riseTime" placeholder="晋级时间"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
export default defineComponent({
  setup() {
    return reactive({
      visible: false,
      dataForm: {
        id: "",
        matchItemId: "",
        teamId: "",
        teamName: "",
        signTime: "",
        isRise: "",
        riseTime: ""
      }
    });
  },
  computed: {
    dataRule() {
      return {
        matchItemId: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        teamId: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        teamName: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        signTime: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        isRise: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
      };
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get("/sp/spmatchitemteam/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/sp/spmatchitemteam", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>
